package com.pomr.model.data;

import java.text.SimpleDateFormat;

import com.pomr.model.equipmentstorage.MonitoringData;

/**
 * 愛奧樂 数据
 * @author Administrator
 *
 */
public class AalData {
	
	public String getDatastr() {
		return datastr;
	}
	public void setDatastr(String datastr) {
		this.datastr = datastr;
	}
	public ObjectData getData() {
		return data;
	}
	public void setData(ObjectData data) {
		this.data = data;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getDay() {
		return day;
	}
	public void setDay(String day) {
		this.day = day;
	}
	public String getHour() {
		return hour;
	}
	public void setHour(String hour) {
		this.hour = hour;
	}
	public String getMinute() {
		return minute;
	}
	public void setMinute(String minute) {
		this.minute = minute;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getVer() {
		return ver;
	}
	public void setVer(String ver) {
		this.ver = ver;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getCustomer() {
		return customer;
	}
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	public String getModelCode() {
		return modelCode;
	}
	public void setModelCode(String modelCode) {
		this.modelCode = modelCode;
	}
	public String getDeviceType() {
		return deviceType;
	}
	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}
	public String getSerialnumber() {
		return serialnumber;
	}
	public void setSerialnumber(String serialnumber) {
		this.serialnumber = serialnumber;
	}
	
	
	public String getSum() {
		return sum;
	}
	public void setSum(String sum) {
		this.sum = sum;
	}
	public String getImsi() {
		return imsi;
	}
	public void setImsi(String imsi) {
		this.imsi = imsi;
	}
	public String getImei() {
		return imei;
	}
	public void setImei(String imei) {
		this.imei = imei;
	}
	public String getRest() {
		return rest;
	}
	public void setRest(String rest) {
		this.rest = rest;
	}
	private String datastr;//上传的字符串
	private String start;//起始码 2
	private String ver;//版本号 2
	private String user;//用户号 1
	private String customer;//客户号 2
	private String modelCode;//机器代码 2
	private String deviceType;//机种码 2
	private String serialnumber;//系列号 9
	private ObjectData data;//数据 9
	private String year;//时间年 2
	private String month;//时间月 2
	private String day;//时间日 2
	private String hour;//时间时2
	private String minute;//时间分 2
	private String sum;//检校码 4
	private String imsi;// _ 15
	private String imei;// _ 15
	private String rest;//保留字符 10
	
	public static AalData getInitializtion(String dataStr){
		
		AalData ad = new AalData();
		ad.datastr = dataStr;
		int startLoc = 0;
		String childStr = null;
		ad.start = dataStr.substring(0, 2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.ver = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.user = childStr.substring(0,1);
		startLoc += 1;
		childStr = dataStr.substring(startLoc);
		ad.customer = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.modelCode = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.deviceType = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.serialnumber = childStr.substring(0,9);
		startLoc += 9;
		childStr = dataStr.substring(startLoc);
		//TODO 数据部分
		if(ad.deviceType.equals("01")){//血压计
			ad.data = GXYData.getInitializtion(childStr.substring(0,9));
		}else if(ad.deviceType.equals("02")){//血糖仪
			ad.data = XTYData.getInitializtion(childStr.substring(0,9));
		}
		startLoc += 9;
		childStr = dataStr.substring(startLoc);
		ad.year = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.month = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.day = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.hour = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.minute = childStr.substring(0,2);
		startLoc += 2;
		childStr = dataStr.substring(startLoc);
		ad.sum = childStr.substring(0,4);
		startLoc += 4;
		childStr = dataStr.substring(startLoc);
		childStr = childStr.replaceFirst("_", "");//去除连接符
		startLoc += 1;
		ad.imsi = childStr.substring(0,15);
		startLoc += 15;
		childStr = dataStr.substring(startLoc);
		childStr = childStr.replaceFirst("_", "");//去除连接符
		startLoc += 1;
		ad.imei = childStr.substring(0,15);
		startLoc += 15;
		childStr = dataStr.substring(startLoc);
		ad.rest = childStr.substring(0,10);
		return ad;
	}
	
	//通过数据字符串解析数据
	public AalData() {
		
	}
	
	
	//重写输出方法
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("上传数据串:"+this.datastr+"\n");
		sb.append("start 起始码 2:"+this.start+"\n");
		sb.append("ver 版本号 2:"+this.ver+"\n");
		sb.append("user 用户号 1:"+this.user+"\n");
		sb.append("customer 客户号 2:"+this.customer+"\n");
		sb.append("modelCode 机器代码 2:"+this.modelCode+"\n");
		sb.append("deviceType 机种码 2:"+this.deviceType+"\n");
		sb.append("serialnumber 系列号 9:"+this.serialnumber+"\n");
		sb.append(this.data.toString());
		sb.append("year 时间年 2:"+this.year+"\n");
		sb.append("month 时间月 2:"+this.month+"\n");
		sb.append("day 时间日 2:"+this.day+"\n");
		sb.append("hour 时间时2:"+this.hour+"\n");
		sb.append("minute 时间分 2:"+this.minute+"\n");
		sb.append("sum 检校码 4:"+this.sum+"\n");
		sb.append("imsi  _ 15:"+this.imsi+"\n");
		sb.append("imei  _ 15:"+this.imei+"\n");
		sb.append("rest 保留字符 10:"+this.rest+"\n");
		return sb.toString();
	}
	
	/**
	 * 获取数据库数据对象
	 * @return
	 */
	public MonitoringData findMonitor() throws Exception{
		MonitoringData md = new MonitoringData();
		md.setData(this.data.toXML());
		md.setImei(this.imei);
		String time = "20"+this.year+"-"+this.month+"-"+this.day+" "+this.hour+":"+this.minute+":"+"00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		md.setMonitorTime(sdf.parse(time));
		return md;
	}
}
